package com.itheima.service.system.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.RoleService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-27 11:33
 */
public class RoleServiceImpl implements RoleService{

    @Override
    public PageInfo findAll(int page,int size) {
        PageInfo PageInfo = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            PageHelper.startPage(page,size);
            List<Role> all = roleDao.findAll();
            PageInfo = new PageInfo(all);
            return PageInfo;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public List<Role> findAll() {

        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);

            List<Role> all = roleDao.findAll();

            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public Role findById(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            Role byId = roleDao.findById(id);
            return byId;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public int delete(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            int delete = roleDao.delete(id);
            TransactionUtil.commit(sqlSession);
            return delete;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public int save(Role role) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            String id = UUID.randomUUID().toString();
            role.setId(id);
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            int save = roleDao.save(role);
            TransactionUtil.commit(sqlSession);
            return save;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public int update(Role role) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            int update = roleDao.update(role);
            TransactionUtil.commit(sqlSession);
            return update;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void updateRoleModule(String roleId, String moduleIds) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            roleDao.deleteRoleModule(roleId);
            //切割字符串,前台传过来的模块为用逗号连接的字符串
            String[] moduleArr = moduleIds.split(",");
            for (String module : moduleArr) {
                roleDao.saveRoleModule(roleId, module);
            }

            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
